package com.iwhalecloud.citybrain.flow.platform.manager.application.config;

import com.iwhalecloud.citybrain.flow.platform.manager.application.listener.RedisMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisPubsubConfiguration {

    @Autowired
    private RedisMessageListener redisMessageListener;

    @Value("${gts.cache.database:0}")
    private String database;

    /**
     * 监听database中所有将要失效的key
     * @return
     */
    @Bean
    public ChannelTopic expiredTopic() {
        return new ChannelTopic("__keyevent@"+database+"__:expired");
    }

    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(
            @Autowired RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(redisMessageListener, expiredTopic());
        return redisMessageListenerContainer;
    }


}
